package com.wise.service.cms.content.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wise.entities.content.Product;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne4Column;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.validation.Precondition;
import com.wise.model.content.enums.ProductState;
import com.wise.summer.v1.cms.content.product.UpdateProductById;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.Date;
import java.util.Objects;

import static com.wise.model.content.ContentErrMsgs.*;

/**
 * @author shaoming
 * @date 2019-09-09
 */
@Service
@SummerService
public class UpdateProductByIdService implements SummerServiceBean<UpdateProductById> {

    private static final Integer LENGTH = 20;

    @Override
    public void sum(UpdateProductById summer) {
        if (summer.getCategoryType() == null) {
            summer.setCategoryType(0);
        }
        //产品标题不可重复
        AddProductService.checkIfDuplicateTitle(summer.getTitle(), summer.getId());
        Product prod = DaoOne4Column.s(Product.class, Product._routeId, summer.getRouteId(), null);
        /* prod不为null且prod.getId()和summer.getId()不相等*/
        Precondition.checkState(Objects.isNull(prod) || Objects.equals(prod.getId(), summer.getId()), Err_001000250032);
        Product product = DaoOne4Update.s(Product.class, summer.getId(), Err_001000250009);

        // 草稿
        if (summer.getDraftState() == null){
            summer.setDraftState(false);
        }
        if (summer.getDraftState()){
            Product draft = copyEntity(summer, product);
            String draftJson = JSON.toJSONString(draft);
            product.setDraft(draftJson);
            DaoSave.s(product);
        }else {
            BeanUtils.copyProperties(copyEntity(summer, product), product);
            product.setDraft(null);
            product.setPublishTime(setPublishTimeByStatus(summer.getState()));
            DaoSave.s(product);
        }

        summer.setSummerResult(product);
    }

    static Date setPublishTimeByStatus(Integer status) {
        return ProductState.UP.isValue(status) ? new Date() : null;
    }

    private Product copyEntity(UpdateProductById summer, Product product){

        Product temp = new Product();
        BeanUtils.copyProperties(product, temp);
        temp.setDraft(null);
        BeanUtils.copyProperties(summer, temp, Product._url);

        JSONArray url = summer.getUrl();
        JSONArray guideUrl = JSONObject.parseArray(summer.getUserGuideUrl());
        JSONArray introUrl = JSONObject.parseArray(summer.getProductIntroUrl());

        if (url.size() > LENGTH) {
            throw new LogicException(this.getClass(), Err_001000250048);
        }
        if (guideUrl.size() > LENGTH) {
            throw new LogicException(this.getClass(), Err_001000250050);
        }
        if (introUrl.size() > LENGTH) {
            throw new LogicException(this.getClass(), Err_001000250049);
        }
        temp.setUrl(summer.getUrl().toJSONString());

        temp.setLastUpdateBy(CurrentUserId.s(true));
        temp.setLastUpdateDate(DateTimeUtil.timestampNow());

        return temp;
    }
}
